// 渲染线程（vue所在线程）vite配置

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'
import eslintPlugin from '@nabla/vite-plugin-eslint'
import Components from 'unplugin-vue-components/vite'
import { ArcoResolver } from 'unplugin-vue-components/resolvers'
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer' // 图片压缩

export default defineConfig(({ command }) => {
  const plugins = [vue()]

  // 开发阶段启用eslint
  if (command === 'serve') {
    plugins.push(
      eslintPlugin({
        eslintOptions: {
          cache: true,
          fix: true,
          extensions: ['src/**/*.ts', 'src/**/*.tsx', 'src/**/*.vue']
        }
      })
    )
  }
  // 生产环境启用组件按需加载
  else {
    plugins.push(
        Components({
          resolvers: [
            ArcoResolver({
              sideEffect: true
            })
          ]
        }),
        ViteImageOptimizer()
    )
  }

  return {
    plugins,
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
      extensions: ['.js', '.ts'],
    },
    css: {
      preprocessorOptions: {
        less: {
          // 添加额外css变量
          additionalData: '@import "node_modules/@arco-design/web-vue/es/style/theme/global.less";'
        }
      }
    },
    build: {
      target: 'esnext',
      chunkSizeWarningLimit: 2000
    }
  }
})
